package com.jblv.frame.web.controller.impl;

import com.jblv.frame.common.controller.BaseController;
import com.jblv.frame.common.dto.PageResult;
import com.jblv.frame.common.message.Result;
import com.jblv.frame.common.utils.StringUtils;
import com.jblv.frame.common.utils.tree.TreeUtils;
import com.jblv.frame.user.api.vo.SysDeptListVO;
import com.jblv.frame.user.api.dto.SysDeptDTO;
import com.jblv.frame.user.api.entity.SysDept;
import com.jblv.frame.user.api.params.SysDeptListParams;
import com.jblv.frame.user.api.params.SysDeptParams;
import com.jblv.frame.web.controller.ISysDeptController;
import com.jblv.frame.web.facade.ISysDeptFacade;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author happyonion.yangtao
 * @version 1.0
 * @title SysDeptController
 * @projectName jblv-frame
 * @packageName com.jblv.web
 * @description SysDeptController 部门Controller实现类
 * @date Create at 2020/5/26 22:24
 * @since jdk1.8
 */
@Api(tags = "部门管理", value = "/v1/api/dept", description = "关于部门相关接口")
@RestController
@RequestMapping("/v1/api/dept")
public class SysDeptController extends BaseController implements ISysDeptController {

    @Autowired
    ISysDeptFacade sysDeptFacade;


    @ApiOperation(value = "获取ID部门信息", response = SysDept.class, httpMethod = "GET", notes = "传入部门信息ID")
    @Override
    @GetMapping("/id/{id}")
    public Result<SysDept> get(@PathVariable("id") String id){
        return renderSuccess(sysDeptFacade.get(id));
    }

    @ApiOperation(value = "获取部门列表", httpMethod = "POST", notes = "获取部门列表说明")
    @Override
    @PostMapping("/list")
    public Result<PageResult<SysDeptDTO>> getList(@RequestBody SysDeptListParams params){
        if(StringUtils.isEmpty(params.getName())){
            return renderSuccess(sysDeptFacade.getList());
        }else{
            return renderSuccess(sysDeptFacade.getList(params));
        }

    }

    @ApiOperation(value = "创建部门", httpMethod = "POST", notes = "传入部门DTO")
    @Override
    @PostMapping("")
    public Result<String> save(@RequestBody SysDeptParams sysDeptParams) {
        return renderSuccess(sysDeptFacade.create(sysDeptParams));
    }

    @ApiOperation(value = "删除部门", httpMethod = "DELETE", notes = "传入部门ID")
    @Override
    @DeleteMapping("/{id}")
    public Result<Boolean> del(@PathVariable String id) {
        return renderSuccess(sysDeptFacade.del(id));
    }

    @ApiOperation(value = "更新部门", httpMethod = "PUT", notes = "传入部门DTO")
    @Override
    @PutMapping("")
    public Result<Boolean> update(@RequestBody SysDeptParams sysDeptParams) {
        return renderSuccess(sysDeptFacade.update(sysDeptParams));
    }

    @Override
    @GetMapping("/getTree")
    public Result<List<SysDeptListVO>> getTree() {
        //List<FacadeSysDeptDTO> tree = GsonUtils.jsonToBean(returnTree, new TypeToken<List<FacadeSysDeptDTO>>() { }.getType());
        List<SysDeptListVO> temp = sysDeptFacade.getTreeList();
        return renderSuccess(temp);
    }
}
